<?php

namespace App\System\Goods\Repositories;


use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Goods\Model\GoodsAlbum;

class GoodsAlbumRepository extends Repository
{
    use SinglePattern;

    /*
     * 商品图片列表
     * $getall FALSE 查询分页数据 TRUE 不分页所有数据
     */
    public function getList($goods_id='',$getall=false)
    {
        if($getall){
            $data = GoodsAlbum::with(['image'])
            ->when($goods_id,function ($query) use($goods_id){
                return $query->where('goods_id',$goods_id);
            })
                ->orderBy('sort')
                ->get();
        }else{
            $data = GoodsAlbum::with(['image'])
                ->when($goods_id,function ($query) use($goods_id){
                return $query->where('goods_id',$goods_id);
            })
                ->orderBy('sort')
                ->paginate();
        }
        $data = $data->toArray();
        return $data;
    }

    /**
     * 通过产品ID数组查找产品主图
     * @param array $goods_ids 产品ID数组
     * @return mixed
     */
    public function getInIds(array $goods_ids)
    {
        $res = GoodsAlbum::with(['image'])
            ->whereIn("goods_id",$goods_ids)
            ->orderBy('sort')
            ->get();
        $data = [];
        if($res)
        {
            foreach ($res as $v)
            {
                if(!isset($data[$v->goods_id]))
                {
                    $data[$v->goods_id] = $v->image->path.$v->image->filename;
                }
            }
        }
        return $data;
    }

}